package olegroshka.src.com.bofa.olegroshka.datasource;
import java.util.Iterator;

import olegroshka.src.com.bofa.olegroshka.format.TemplateFormatter;

public class FormattingDataSource implements DataSource<String> {

	private final DataSource<Object[]> dataSource;
	private final TemplateFormatter formatter;
	

	public FormattingDataSource(
			DataSource<Object[]> dataSource,
			TemplateFormatter formatter) {
		this.dataSource = dataSource;
		this.formatter = formatter;
	}

	public Iterator<String> iterator() {
		return new Iterator<String>() {
			
			private final Iterator<Object[]> iterator = dataSource.iterator();
			
			public boolean hasNext() {
				return iterator.hasNext();
			}

			public String next() {
				Object[] inputData = iterator.next();
				
				String result = null;
				if( inputData != null ) {
					result = formatter.decorate(inputData);
				}
				
				return result;
			}

			public void remove() {
				throw new UnsupportedOperationException();
			}
		};
	}

	public void begin() {
		dataSource.begin();
	}

	public void end() {
		dataSource.end();
	}

}
